<?php
// Functions backported from WP 2.2

function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
	global $wpdb;

	$post_ID = (int) $post_ID;
	// If $post_categories isn't already an array, make it one:
	if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
		$post_categories = array(get_option('default_category'));

	$post_categories = array_unique($post_categories);

	// First the old categories
	$old_categories = $wpdb->get_col("
		SELECT category_id
		FROM $wpdb->post2cat
		WHERE post_id = '$post_ID'");

	if (!$old_categories) {
		$old_categories = array();
	} else {
		$old_categories = array_unique($old_categories);
	}

	// Delete any?
	$delete_cats = array_diff($old_categories,$post_categories);

	if ($delete_cats) {
		foreach ($delete_cats as $del) {
			$wpdb->query("
				DELETE FROM $wpdb->post2cat
				WHERE category_id = '$del'
					AND post_id = '$post_ID'
				");
		}
	}

	// Add any?
	$add_cats = array_diff($post_categories, $old_categories);

	if ($add_cats) {
		foreach ($add_cats as $new_cat) {
			$new_cat = (int) $new_cat;
			if ( !empty($new_cat) )
				$wpdb->query("
					INSERT INTO $wpdb->post2cat (post_id, category_id) 
					VALUES ('$post_ID', '$new_cat')");
		}
	}

	// Update category counts.
	$all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
	foreach ( $all_affected_cats as $cat_id ) {
		$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND category_id = '$cat_id'");
		$wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
		clean_category_cache($cat_id);
		do_action('edit_category', $cat_id);
	}
}	// wp_set_post_categories()

function clean_category_cache($id) {
	wp_cache_delete($id, 'category');
	wp_cache_delete('all_category_ids', 'category');
	wp_cache_delete('get_categories', 'category');
	delete_option('category_children');
}

?>
